#!/usr/bin/perl -w
use strict;
use v5.10;
use lib '/data/workarea/corpasm/src';
use modules::utilities;
use modules::tophat;
use modules::cufflinks;
use modules::cuffmerge;
use modules::cuffdiff;
use modules::cuffcompare;
use Data::Dumper;
print "\n\nWelcome to TGAC's RNA-seq pipeline!\n\n";

my $time_start    = time;
my $conf_file     = "/usr/users/ga002/corpasm/workarea/src/conf/arab3.txt";
my $util          = modules::utilities->new();
my $config        = $util->read_conf_file($conf_file,'file_paths');


my @jobs_to_run   = split /[,]/, $config->{'JOBS_TO_RUN'};

foreach my $job (@jobs_to_run) {

  run_tophat(      $config ) if $job ==1;

  run_cufflinks(   $config ) if $job ==2;

  run_cuffmerge(   $config ) if $job ==3;

  run_cuffdiff(    $config ) if $job ==4;
  
  run_cuffcompare( $config ) if $job ==5;
}

my $time_taken = time - $time_start;
print "\n\nTGAC RNA-seq pipeline finished.\n";
print "TIME ELAPSED: $time_taken seconds\n";

sub run_tophat {
  my ($config) = @_;

  my $oTophat = modules::tophat->new( $config );    
     $oTophat->prepare();
  my $out = $oTophat->run();
  return $out;
}

sub run_cufflinks {
  my ($config) = @_;
  
  my $oCufflinks = modules::cufflinks->new( $config );    
     $oCufflinks->prepare();
     $oCufflinks->run( $config->{'TOPHAT_OUT'} );
  
  return;
}

sub run_cuffmerge {
  my ($config) = @_;

  my $oCuffmerge  = modules::cuffmerge->new( $config );    
     $oCuffmerge->prepare( $config->{'CUFFLINKS_OUT'} );
     $oCuffmerge->run();
  return;
}

sub run_cuffdiff {
  my ($config,$tophat_dir,$cuffmerge_dir) = @_;

  my $oCuffdiff = modules::cuffdiff->new( $config,$config->{'TOPHAT_OUT'} );
     $oCuffdiff->prepare( $config->{'CUFFMERGE_OUT'} );
     $oCuffdiff->run();
  return;
}

sub run_cuffcompare {
  my ($config) = @_;

  my $oCuffcompare = modules::cuffcompare->new( $config );
     $oCuffcompare->prepare();
     $oCuffcompare->run();
  return;
}




# INVOKE AND RUN PACKAGES SPECIFIED IN AN ARRAY
#foreach my $prog (@progs) {
#  my ($oProg,$out);
#  my $package = "modules::$prog";
#  $oProg = $package->new($config);
#  $out   = $oProg->prepare();
#  $oProg->run($out);
#}

# OPEN FILES IN DIRECTORY
#opendir(my $dh, $fastq_dir) || die "can't opendir $fastq_dir: $!";
#my @fastq_files = grep { /\.fastq/ && -f "$fastq_dir/$_" } readdir($dh);
#closedir $dh;




